﻿@using Fluxor.Blazor.Web.Components;
@using SimpleIdServer.IdServer.UI;
@using SimpleIdServer.IdServer.Website.Models;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.ClientStore;
@using SimpleIdServer.IdServer.Website.Stores.ScopeStore;
@using SimpleIdServer.IdServer.Website.Stores.UserStore;
@using System.Reflection;
@inherits FluxorComponent
@inject IState<UpdateScopeMapperState> updateScopeMapperState
@inject IDispatcher dispatcher

<RadzenTemplateForm Submit=@SubmitScopeMapper TItem="UserPropertyScopeMapperForm" Data=@scopeMapperFom>
    @if (!updateScopeMapperState.Value.IsUpdating && !string.IsNullOrWhiteSpace(updateScopeMapperState.Value.ErrorMessage))
    {
        <RadzenAlert AllowClose="false" AlertStyle="AlertStyle.Danger" ShowIcon="true" Variant="Variant.Flat" Shade="Shade.Lighter">@updateScopeMapperState.Value.ErrorMessage</RadzenAlert>
    }

    <UserScopeMapper UserScopeMapperForm="@scopeMapperFom" Scope="@Scope" IsEditModeEnabled="@IsEditModeEnabled"></UserScopeMapper>
    <!-- Property -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.UserProperty</RadzenText>
        <RadzenDropDown Name="SourceUserPropertyName" Class="w-100"
                        Data=@userProperties
                        TValue="String"
                        @bind-Value=@scopeMapperFom.SourceUserPropertyName />
        <p class="text-muted">Name of the property</p>
        <RadzenRequiredValidator Component="SourceUserPropertyName" Text="@Global.UserPropertyRequired"></RadzenRequiredValidator>
    </div>
    <!-- Include the claims into the access token -->
    <div class="mt-1">
        <RadzenSwitch @bind-Value=@scopeMapperFom.IncludeInAccessToken Name="IncludeInAccessToken" TValue="bool" />
        <RadzenLabel Text="@Global.IncludeInAccessToken" Component="IncludeInAccessToken" Style="margin-left: 8px; vertical-align: middle;" />
    </div>
    @if(IsEnabled)
    {
        <RadzenButton class="mt-1" Variant="Variant.Flat" ButtonType="ButtonType.Submit" ButtonStyle="ButtonStyle.Success" Text="@(updateScopeMapperState.Value.IsUpdating ? Global.Saving : Global.Save)" Disabled="@(updateScopeMapperState.Value.IsUpdating)" />
    }
</RadzenTemplateForm>
@code {
    [Parameter]
    public EventCallback<ScopeClaimMapper> ScopeMapperSaved { get; set; }
    [Parameter]
    public ScopeClaimMapper ClaimMapper { get; set; } = null!;
    [Parameter]
    public Scope Scope { get; set; } = null!;
    [Parameter]
    public bool IsEditModeEnabled { get; set; } = false;
    [Parameter]
    public bool IsEnabled { get; set; } = true;

    IEnumerable<string> userProperties = new List<string>();

    record UserPropertyScopeMapperForm : BaseUserScopeMapperForm
    {
        public string SourceUserPropertyName { get; set; } = null!;
    }

    UserPropertyScopeMapperForm scopeMapperFom = new UserPropertyScopeMapperForm();

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        userProperties = typeof(User).GetProperties(BindingFlags.Instance | BindingFlags.Public)
                .Where(p =>
                {
                    var attr = p.GetCustomAttribute<UserPropertyAttribute>();
                    return attr == null ? false : attr.IsVisible;
                })
                .Select(p => p.Name);
        if (ClaimMapper == null) return;
        scopeMapperFom.Update(ClaimMapper);
        scopeMapperFom.SourceUserPropertyName = ClaimMapper.SourceUserProperty;
    }

    async void SubmitScopeMapper()
    {
        var result = scopeMapperFom.Build();
        result.MapperType = MappingRuleTypes.USERPROPERTY;
        result.SourceUserProperty = scopeMapperFom.SourceUserPropertyName;
        await ScopeMapperSaved.InvokeAsync(result);
    }
}